package elitech.vietnam.myfashion.extend;

import java.util.List;
import java.util.Locale;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;
import elitech.vietnam.myfashion.entites.Category;
import elitech.vietnam.myfashion.fragment.ProductFragment;
import elitech.vietnam.myfashion.view.ItemSubView;

public class GridCategoriesArrayAdapter extends ArrayAdapter<Category> {
	private MainActivity mContext;

	public GridCategoriesArrayAdapter(MainActivity context, int resource,
			int textViewResourceId, List<Category> objects) {
		super(context, resource, textViewResourceId, objects);
		this.mContext = context;
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}

	static class ViewHolder {
		ImageView image;
		TextView txtTitle;
		LinearLayout layCate;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final ViewHolder holder;
		final Category category = getItem(position);
		if (row == null) {
			LayoutInflater inflater = mContext.getLayoutInflater();
			row = inflater.inflate(R.layout.category, parent, false);
			holder = new ViewHolder();

			holder.image = (ImageView) row.findViewById(R.id.cate_image);
			holder.txtTitle = (TextView) row.findViewById(R.id.cate_title);
			holder.layCate = (LinearLayout) row.findViewById(R.id.cate_layout);
			row.setTag(holder);
		} else
			holder = (ViewHolder) row.getTag();

		if (category.isTrademark) {

			UrlImageViewHelper.setUrlDrawable(holder.image,
					Const.SERVER_IMAGE_THUMB_URL + category.TrademarksImage);

			holder.txtTitle.setText(category.TrademarksName);
			holder.layCate.removeAllViews();
			for (final Category subCategory : category.subCategories) {
				ItemSubView item = new ItemSubView(mContext, subCategory);
				item.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (mContext.mProductFragment != null)
							if (mContext.mProductFragment.isAdded())
								return;

						mContext.mProductCategory = subCategory;
						mContext.mProductFragment = new ProductFragment();
						mContext.replaceFragment(mContext.mProductFragment,
								true);
					}
				});
				holder.layCate.addView(item);
			}

			row.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (holder.layCate.getVisibility() == View.VISIBLE) {
						holder.layCate.setVisibility(View.GONE);
					} else {
						holder.layCate.setVisibility(View.VISIBLE);
					}
				}
			});
		} else {

			UrlImageViewHelper.setUrlDrawable(holder.image,
					Const.SERVER_IMAGE_THUMB_URL + category.Image);

			if (mContext.getLocale().equals(Locale.ENGLISH))
				holder.txtTitle.setText(category.NameEN);
			else if (mContext.getLocale().equals(Locale.KOREA))
				holder.txtTitle.setText(category.NameKR);
			else
				holder.txtTitle.setText(category.NameVN);

			row.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (mContext.mProductFragment != null)
						if (mContext.mProductFragment.isAdded())
							return;

					mContext.mProductCategory = category;
					mContext.mProductFragment = new ProductFragment();
					mContext.replaceFragment(mContext.mProductFragment, true);
				}
			});
		}

		return row;
	}

}
